package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 客户管理
 * @author: 化一帆
 * @date: 2023/2/10 10:01
 * @version: v1.0
 */
@Service
@SuppressWarnings("all")
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String customerName) {
        HashMap<String, Object> map = new HashMap<>();

        page=page==0?1:page;
        //起始索引
        int startPage=(page-1)*rows;

        //分页+模糊查询
        List<Customer> customerList=customerDao.getList(startPage,rows,customerName);

        //计数
        Integer count=customerDao.count();
        map.put("rows",customerList);
        map.put("total", count);
        return map;
    }

    /**
     * @description: 客户的添加和修改的接口
     * @author 化一帆
     * @date 2023/2/10 10:58
     */

    @Override
    public ServiceVO save(Customer customer) {


        if(customer.getCustomerId()==null){
           //添加
            customerDao.insert(customer);
        }else{
            //修改
            customerDao.update(customer);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * @description: 删除{可以批量删除}
     * @author 化一帆
     * @date 2023/2/10 11:22
     */
    @Override
    public ServiceVO delete(String ids) {

        //将字符串分割变为数组
        String[] idArray = ids.split(",");
        for (String s : idArray) {
            customerDao.delete(s);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
}
